package com.ywu.container.standard.api;

import com.ywu.boot.context.YwuContext;
import com.ywu.boot.context.YwuContextHolder;
import com.ywu.common.result.CommonResult;
import com.ywu.container.standard.dto.ContainerLogDto;
import com.ywu.container.standard.dto.ServiceContainerDto;
import com.ywu.container.standard.service.ServiceContainerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @ClassName ServiceContainerApi
 * @Description TODO
 * @Author GroundDemo
 * @Date 2024/3/4 21:16
 * @Version 1.0
 **/
@RestController
@RequestMapping("/ywu/container")
public class ServiceContainerApi {
    @Autowired
    private ServiceContainerService service;

    /**
    * @Author GroundDemo
    * @Description 查询容器
    * @Date 22:12 2024/3/6
    * @Param []
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @PostMapping("/container.query.info")
    public CommonResult<Object> queryContainer(@RequestBody ServiceContainerDto serviceContainerDto) {
        return service.queryContainer(serviceContainerDto);
    }

    /**
    * @Author GroundDemo
    * @Description 创建容器
    * @Date 21:19 2024/3/4
    * @Param [serviceContainerDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @PostMapping("/container.create.info")
    private CommonResult<Object> createContainer(@RequestBody ServiceContainerDto serviceContainerDto) {
        return service.createContainer(serviceContainerDto);
    }

    /**
    * @Author GroundDemo
    * @Description 更新容器
    * @Date 21:34 2024/3/6
    * @Param [serviceContainerDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @PostMapping("/container.update.info")
    private CommonResult<Object> updateContainer(@RequestBody ServiceContainerDto serviceContainerDto) {
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        service.updateContainer(serviceContainerDto, currentContext);
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 构建项目
    * @Date 20:11 2024/3/6
    * @Param [serviceContainerDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @PostMapping("/container.build.info")
    private CommonResult<Object> buildContainer(@RequestBody ServiceContainerDto serviceContainerDto) {
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        // 异步执行构建
        service.buildContainer(serviceContainerDto, currentContext);
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 推包
    * @Date 20:24 2024/3/6
    * @Param [serviceContainerDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @PostMapping("/container.push.info")
    private CommonResult<Object> pushPackage(@RequestBody ServiceContainerDto serviceContainerDto) {
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        // 异步执行推包
        service.pushPackage(serviceContainerDto, currentContext);
        return CommonResult.success(null);
    }

    /**
     * @Author GroundDemo
     * @Description 部署容器
     * @Date 21:19 2024/3/4
     * @Param [serviceContainerDto]
     * @return com.ywu.common.result.CommonResult<java.lang.Object>
     **/
    @PostMapping("/container.deploy.info")
    private CommonResult<Object> deployContainer(@RequestBody ServiceContainerDto serviceContainerDto) {
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        // 异步执行部署
        service.deployContainer(serviceContainerDto, currentContext);
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 重启容器
    * @Date 21:19 2024/3/6
    * @Param [serviceContainerDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @PostMapping("/container.restart.info")
    private CommonResult<Object> restartContainer(@RequestBody List<ServiceContainerDto> serviceContainerDtos) {
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        // 异步执行重启
        service.restartContainer(serviceContainerDtos, currentContext);
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 删除容器
    * @Date 21:27 2024/3/6
    * @Param [serviceContainerDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @PostMapping("/container.delete.info")
    public CommonResult<Object> deleteContainer(@RequestBody ServiceContainerDto serviceContainerDto) {
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        // 异步删除容器
        service.deleteContainer(serviceContainerDto, currentContext);
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 一键部署容器
    * @Date 21:45 2024/3/6
    * @Param [serviceContainerDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @PostMapping("/container.simple.deploy.info")
    public CommonResult<Object> simpleDeployContainer(@RequestBody ServiceContainerDto serviceContainerDto) {
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        // 异步部署
        service.simpleDeployContainer(serviceContainerDto, currentContext);
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 查询容器的日志
    * @Date 0:19 2024/3/9
    * @Param []
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @PostMapping("/container.logs.info")
    public CommonResult<Object> containerInfoLogs(@RequestBody ServiceContainerDto serviceContainerDto) {
        return service.containerInfoLogs(serviceContainerDto);
    }

    /**
    * @Author GroundDemo
    * @Description 查询流水线步骤信息
    * @Date 23:10 2024/4/13
    * @Param [serviceContainerDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @PostMapping("/container.build.step.info")
    public CommonResult<Object> queryStepInfo(@RequestBody ServiceContainerDto serviceContainerDto) {
        return service.queryStepInfo(serviceContainerDto);
    }

    /**
    * @Author GroundDemo
    * @Description 根据containerId批量查询容器详情
    * @Date 20:22 2024/5/22
    * @Param [serviceContainerDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @PostMapping("/container.query.instance.detail.info")
    public CommonResult<Object> containerDetailsQuery(@RequestBody ServiceContainerDto serviceContainerDto) {
        return service.containerDetailsQuery(serviceContainerDto);
    }

    /**
    * @Author GroundDemo
    * @Description 查询容器的日志文件列表 - 只展示名称和文件大小
    * @Date 10:08 2025/3/26
    * @Param [serviceContainerDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @PostMapping("/container.app.log.list.info")
    public CommonResult<Object> queryContainerAppLogList(@RequestBody ServiceContainerDto serviceContainerDto) {
        return service.queryContainerAppLogList(serviceContainerDto);
    }

    /**
    * @Author GroundDemo
    * @Description 下载应用日志
    * @Date 12:16 2025/3/26
    * @Param [containerLogDto, response]
    * @return void
    **/
    @PostMapping("/container.app.log.info.download")
    public void downloadAppLog(@RequestBody ContainerLogDto containerLogDto, HttpServletResponse response) {
        service.downloadAppLog(containerLogDto, response);
    }
}
